package cn.kduck.module.user.query;

import cn.kduck.core.dao.definition.BeanDefDepository;
import cn.kduck.core.dao.definition.BeanEntityDef;
import cn.kduck.core.dao.query.QueryCreator;
import cn.kduck.core.dao.query.QuerySupport;
import cn.kduck.core.dao.sqlbuilder.ConditionBuilder.ConditionType;
import cn.kduck.core.dao.sqlbuilder.SelectBuilder;
import org.springframework.stereotype.Component;

import java.util.Map;

import static cn.kduck.module.user.service.UserService.CODE_USER;

/**
 * 判断用户是否已经存在，主要检查手机号或者邮箱地址是否重复
 */
@Component
public class ExistUserQuery implements QueryCreator {

    @Override
    public QuerySupport createQuery(Map<String, Object> paramMap, BeanDefDepository depository) {
        BeanEntityDef userEntity = depository.getEntityDef(CODE_USER);
        SelectBuilder selectBuilder = new SelectBuilder(userEntity,paramMap);
        selectBuilder.where().and("user_id",ConditionType.NOT_EQUALS,"userId")
                .groupBegin("email", ConditionType.EQUALS,"email")
                .or("phone",ConditionType.EQUALS,"phone",true)
                .groupEnd();
        return selectBuilder.build();
    }
}
